﻿/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1
 *
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations
 * under the License.
 *
 * The Original Code is tyzoh.jp code.
 *
 * The Initial Developer of the Original Code is Kazutaka Kurihara.
 * Portions created by the Initial Developer are Copyright (C) 2007
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *
 * ***** END LICENSE BLOCK ***** */
using System;
using System.Windows.Forms;
using PluginInterface;
using Qurihara;
using System.Collections;
using System.Drawing;
using Microsoft.Ink;
using Qurihara.QBasic;
using ToolBarDock;
using System.Reflection;
using System.IO;

namespace KotodamaPlugin.ViewWritePlugin
{
	/// <summary>
	/// Class1 の概要の説明です。
	/// </summary>
	public class ViewWritePlugin :IPlugin
	{
        private MenuItem writeEraseMenu;
        private bool isWriteMode = true;

        private ViewEraseMode viewEraseMode;
        public ViewEraseMode ViewEraseMode
        {
            get { return viewEraseMode; }
        }

        private ViewWriteMode viewWriteMode;
        public ViewWriteMode ViewWriteMode
        {
            get { return viewWriteMode; }
        }

		ToolBarDock.ToolBarDockHolder holder = null;

		public ViewWritePlugin()
		{
            writeEraseMenu = new MenuItem();
            UpdateModeMenu();
            writeEraseMenu.Click += new EventHandler(writeEraseMenu_Click);
		}

        void writeEraseMenu_Click(object sender, EventArgs e)
        {
            isWriteMode = !isWriteMode;
            UpdateModeMenu();
            if (isWriteMode)
            {
                viewWriteMode.ModeChange();
            }
            else
            {
                ViewEraseMode.ModeChange();
            }
        }

        private bool IsLanguage(string twoLetterISOLanguageName)
        {
            return (twoLetterISOLanguageName ==
                System.Threading.Thread.CurrentThread.CurrentUICulture.TwoLetterISOLanguageName);
        }

        private void UpdateModeMenu()
        {
            string str = "";
            if (isWriteMode) str =  IsLanguage("en") ? "[Write]/Erase" : "[書く]/消す";
            else str = IsLanguage("en") ? "Write/[Erase]" : "書く/[消す]";
            writeEraseMenu.Text = str;
        }

		#region IPlugin Members
		
		IPluginHost myPluginHost = null;
		string myPluginName = "ViewWrite Plugin";
		string myPluginAuthor = "K.Qurihara";
		string myPluginDescription = "test plugin";
		string myPluginVersion = "1.0.0";
		
		public void Initialize()
		{
            ToolBar defaultBar = null;
            holder = null;
            ToolBarDock.ToolBarManager tbm = (ToolBarDock.ToolBarManager)Host.Kotodama.EnvironmentTable["Base.ToolBarManager"];
			if ( tbm != null) 
			{
				holder = tbm.GetHolder("Default");
				defaultBar = (ToolBar)holder.Control;
            }
            else
            {
                defaultBar = (ToolBar)Host.Kotodama.EnvironmentTable["Base.ToolBar"];
            }
            if (defaultBar != null)
            {
                tbm.RemoveControl(defaultBar);
                defaultBar.Dispose();

                ToolBar snapBar = (ToolBar)(tbm.GetHolder("Snap")).Control;
                if (snapBar != null)
                {
                    tbm.RemoveControl(snapBar);
                    snapBar.Dispose();
                }
            }

            //必ず強制的にこのモード。
            ViewWriteMode mode = new ViewWriteMode(Host.Kotodama);
            viewWriteMode = mode;
            mode.ModeChange();
            
            ViewEraseMode modeE = new ViewEraseMode(Host.Kotodama);
            viewEraseMode = modeE;
            
            //modeE.ModeChange();

            Menu.MenuItemCollection mic = (Menu.MenuItemCollection)Host.Kotodama.EnvironmentTable["Base.MenuItems"];
            mic.Add(writeEraseMenu);

        }
        
		void PluginInterface.IPlugin.Dispose()
		{
            Menu.MenuItemCollection mic = (Menu.MenuItemCollection)Host.Kotodama.EnvironmentTable["Base.MenuItems"];
            mic.Remove(writeEraseMenu);
		}

		public string Description
		{
			get
			{
				// TODO:  Add ctlMain.Description getter implementation
				return myPluginDescription;
			}
		}

		public string Author
		{
			get
			{
				// TODO:  Add ctlMain.Author getter implementation
				return myPluginAuthor;
			}
		}

		public IPluginHost Host
		{
			get
			{
				// TODO:  Add ctlMain.Host getter implementation
				return myPluginHost;
			}
			set
			{
				myPluginHost = value;
			}
		}


		
		public string Name
		{
			get
			{
				// TODO:  Add ctlMain.Name getter implementation
				return myPluginName;
			}
		}
		

		public string Version
		{
			get
			{
				// TODO:  Add ctlMain.Version getter implementation
				return myPluginVersion;
			}
		}

		#endregion

	}

}
